package com.xkdx.caipiao.cost;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.xkdx.caipiao.R;
import com.xkdx.caipiao.RechargeActivity;
import com.xkdx.caipiao.ThreadActivity;
import com.xkdx.caipiao.module.network.AbsModule;
import com.xkdx.caipiao.my.PhoneDetailActivity;
import com.xkdx.caipiao.presistence.cost.CostAction;
import com.xkdx.caipiao.presistence.cost.CostModule;
import com.xkdx.caipiao.presistence.cost.CostPresistence;
import com.xkdx.caipiao.presistence.costlist.CostListAction;
import com.xkdx.caipiao.presistence.costlist.CostListInfo;
import com.xkdx.caipiao.presistence.costlist.CostListModule;
import com.xkdx.caipiao.presistence.costlist.CostListPresistence;
import com.xkdx.caipiao.presistence.login.Login;
import com.xkdx.caipiao.presistence.login.LoginInfo;
import com.xkdx.caipiao.statics.IConstants;
import com.xkdx.caipiao.util.IsMobileNum;
import com.xkdx.caipiao.util.Md5Utils;

public class CostRechargeActivty extends ThreadActivity {
	LoginInfo info;
	private CostAction action;
	private CostModule module;
	private CostPresistence presistence;

	private CostListAction listaction;
	private CostListModule listmodule;
	private CostListPresistence listpresistence;
	private List<CostListInfo> list;
	private String devicetype = "2";
	private String proid;
	boolean isVisiable = false;
	private EditText et_phone_num;
	private LinearLayout see_item_number;
	private Button sure_pay;
	TextView tv_money;
	ListView listView;
	ImageView arrow;
	MyAdapter adapter;
	private String numb;
	private String code = "1";
	public static LoginInfo loginInfo;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_cost_caipiao);
		init();
		startThread();
		list = new ArrayList<CostListInfo>();

	}

	private void init() {
		et_phone_num = (EditText) findViewById(R.id.et_phone_num);
		tv_money = (TextView) findViewById(R.id.tv_money);
		
		/*
		 * cmcc =
		 * "^1(34[0-8]|(3[5-9]|47|5[0-2]|57[124]|5[89]|8[2378])\\d)\\d{7}$";
		 * cdma = "^1(3[0-2]|45|5[56]|8[56])\\d{8}$"; wcdma =
		 * "^1(33|53|8[09])\\d{8}$";
		 */
		sure_pay = (Button) findViewById(R.id.sure_pay);
		listView = (ListView) findViewById(R.id.lv_list);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				CostListInfo info = (CostListInfo) adapter.getItem(arg2);
				float mon1 = Float.valueOf(info.getValue()) / 100;
				DecimalFormat df = new DecimalFormat("#0.00");
				tv_money.setText(String.valueOf(df.format(mon1))+"元");
				arrow.setImageResource(R.drawable.down_arrow_more_caipiao);
				listView.setVisibility(View.GONE);
				proid = info.getId();
				// 如果点击的这个是红的将其改变为白色的
				/*if (listmodule.list.get(arg2).isChecked) {
					listmodule.list.get(arg2).isChecked = false;
				} else {// 如果点击的是白的，先将所有变白
					for (CostListInfo hb : listmodule.list) {
						hb.isChecked = false;
					}
					listmodule.list.get(arg2).isChecked = true;
				}
				adapter.notifyDataSetChanged();*/

			}
		});
		sure_pay.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				numb = et_phone_num.getText().toString();
				boolean isnum = IsMobileNum.isMobileNum(numb);
				String info = IsMobileNum.isMobileInfo(numb);
				if ("".equals(numb)) {
					Toast.makeText(CostRechargeActivty.this, "手机号码不能为空", 1)
							.show();
					return;
				}
				if (isnum == false) {
					Toast.makeText(CostRechargeActivty.this, "请输入正确的手机号码", 1)
							.show();
					return;
				}
				if ("联通".equals(info)) {
					Toast.makeText(CostRechargeActivty.this, "只支持移动和电信充值", 1)
					.show();
					return;
				}
				if ("".equals(proid)) {
					Toast.makeText(CostRechargeActivty.this, "请选择要充值的金额", 1)
							.show();
				}
				loginInfo = getLoginInfo();
				if (loginInfo != null
						&& !(TextUtils.isEmpty(loginInfo.getLogintoken()))) {
					if (numb != null && proid != null) {
						startThreadCost();
					}
				} else {
					Intent inten3 = new Intent(CostRechargeActivty.this, Login.class);
					startActivity(inten3);
				}
				

			}
		});
		arrow = (ImageView) findViewById(R.id.im_arrow);
		see_item_number = (LinearLayout) findViewById(R.id.see_item_number);
		see_item_number.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				if (isVisiable) {

					isVisiable = false;
					arrow.setImageResource(R.drawable.down_arrow_more_caipiao);
					listView.setVisibility(View.GONE);
				} else {
					isVisiable = true;
					arrow.setImageResource(R.drawable.up_arrow_more_caipiao);
					listView.setVisibility(View.VISIBLE);
				}

			}

		});
	}

	public void onBackClick(View v) {
		finish();
	}

	@Override
	public void showOnPost(HashMap<String, AbsModule> result) {
		dataLoadDialog.dismiss();
		if (result.containsKey("0")) {
			adapter = new MyAdapter(listmodule.list);
			listView.setAdapter(adapter);

		}
		if (isVisiable == true) {
			code = module.code;
			if ("0000".equals(code)) {
				Toast.makeText(CostRechargeActivty.this, "提交成功", 1).show();
				Intent inte = new Intent(this, PhoneDetailActivity.class);
				startActivity(inte);
			} else if ("1014".equals(code)) {
				Toast.makeText(CostRechargeActivty.this, "金额不足，请先充值", 1).show();
				Intent intent = new Intent(this, RechargeActivity.class);
				startActivity(intent);
			}
		}

	}

	class Holder {
		TextView  tv_ye;

	}

	class MyAdapter extends BaseAdapter {

		private List<CostListInfo> list2;

		public MyAdapter(List<CostListInfo> list2) {
			this.list2 = list2;
		}

		public int getCount() {
			return list2.size();
		}

		@Override
		public Object getItem(int position) {
			return list2.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {
			Holder holder = null;
			if (convertView == null) {
				holder = new Holder();
				convertView = LayoutInflater.from(getApplicationContext())
						.inflate(R.layout.phone_number_caipiao, null);
			
				holder.tv_ye = (TextView) convertView.findViewById(R.id.tv_ye);
				
				convertView.setTag(holder);
			} else {
				holder = (Holder) convertView.getTag();
			}
			float mon1 = Float.valueOf(list2.get(position).getValue()) / 100;
			DecimalFormat df = new DecimalFormat("#0.00");
			String money = df.format(mon1);
			holder.tv_ye.setText(money + "元");
			return convertView;
		}

	}

	public void startThread() {
		if (!(dataLoadDialog.isShowing())) {
			dataLoadDialog.show();
			dataLoadDialog.setContentView(R.layout.progressbar_caipiao);

			listaction = new CostListAction();
			listmodule = new CostListModule();
			listpresistence = new CostListPresistence(this);
			listpresistence.setActitons(listaction);
			listpresistence.setModule(listmodule);
			listpresistence.execute();

		}
	}

	public void startThreadCost() {
		if (!(dataLoadDialog.isShowing())) {
			dataLoadDialog.show();
			dataLoadDialog.setContentView(R.layout.progressbar_caipiao);
			isVisiable = true;
			LoginInfo info = getLoginInfo();
			String md5 = info.getId()+info.getLogintoken()+IConstants.key;
			String signcode = Md5Utils.md5(md5);
			action = new CostAction(info.getId(), info.getLogintoken(),
					devicetype, proid, numb,signcode);
			module = new CostModule();
			presistence = new CostPresistence(this);
			presistence.setActitons(action);
			presistence.setModule(module);
			presistence.execute();

		}
	}

}
